/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.heron.common.basics;

import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;
import java.time.Duration;
import java.util.Random;

import org.junit.Assert;
import org.junit.Test;


public class SysUtilsTest {
  private static final int NUM_ATTEMPTS = 100;

  @Test
  public void testFreePort() throws IOException {
    // Randomized test
    for (int i = 0; i < NUM_ATTEMPTS; ++i) {
      int port = SysUtils.getFreePort();

      // verify that port is free
      new ServerSocket(port).close();
    }
  }

  @Test
  public void testSleep() {
    for (int i = 0; i < NUM_ATTEMPTS; i++) {
      // The value can not be negative
      Duration expectedSleepTime = Duration.ofMillis(new Random().nextInt(100));
      long start = System.currentTimeMillis();
      SysUtils.sleep(expectedSleepTime);
      long end = System.currentTimeMillis();
      Assert.assertTrue((end - start) >= expectedSleepTime.toMillis());
    }
  }

  @Test
  public void testCloseIgnoringException() {
    Closeable closeable = new Closeable() {
      @Override
      public void close() throws IOException {
        throw new RuntimeException("Deliberate exception to be ignored.");
      }
    };

    // This invocation should not throw any exceptions
    // Otherwise, the test will automatically fail
    SysUtils.closeIgnoringExceptions(closeable);
    Assert.assertTrue(true);
  }
}
